//
//  UIImageView+SwiftBox.swift
//  QSwiftBox
//
//  Created by carefree on 2020/3/3.
//  Copyright © 2020 Carefree. All rights reserved.
//

import UIKit

extension QSwiftBox where Base: UIImageView {
    public func drawHorizontalDash(in size: CGSize, lineWidth: CGFloat = 2, length: CGFloat = 2, spacing: CGFloat = 2, color: UIColor = UIColor(0xE5E5E5,1)) {
        UIGraphicsBeginImageContext(size)
        base.image?.draw(in: CGRect(origin: .zero, size: size))
        //获取上下文
        let line = UIGraphicsGetCurrentContext()
        //设置线条终点的形状
        line?.setLineCap(.round)
        //虚线颜色
        line?.setStrokeColor(color.cgColor)
        //虚线粗细宽度
        line?.setLineWidth(lineWidth)
        //设置虚线的长度和间距
        line?.setLineDash(phase: 0, lengths: [length, spacing])
        //设置轨迹
        line?.move(to: CGPoint(x: 0, y: 0))
        line?.addLine(to: CGPoint(x: size.width, y: 0))
        line?.strokePath()
        base.image = UIGraphicsGetImageFromCurrentImageContext()
    }
    
    public func drawVerticalDash(in size: CGSize, lineWidth: CGFloat = 2, length: CGFloat = 2, spacing: CGFloat = 2, color: UIColor = UIColor(0xE5E5E5,1)) {
        UIGraphicsBeginImageContext(size)
        base.image?.draw(in: CGRect(origin: .zero, size: size))
        //获取上下文
        let line = UIGraphicsGetCurrentContext()
        //设置线条终点的形状
        line?.setLineCap(.round)
        //虚线颜色
        line?.setStrokeColor(color.cgColor)
        //虚线粗细宽度
        line?.setLineWidth(lineWidth)
        //设置虚线的长度和间距
        line?.setLineDash(phase: 0, lengths: [length, spacing])
        //设置轨迹
        line?.move(to: CGPoint(x: 0, y: 0))
        line?.addLine(to: CGPoint(x: 0, y: size.height))
        line?.strokePath()
        base.image = UIGraphicsGetImageFromCurrentImageContext()
    }
}
